package com.hubstc.book_borrow.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hubstc.book_borrow.mapper.BookMapper;
import com.hubstc.book_borrow.pojo.Book;
import com.hubstc.book_borrow.pojo.PageBean;
import com.hubstc.book_borrow.service.BookService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookServiceImpl implements BookService {

    @Resource
    private BookMapper bookMapper;

    @Override
    public void addBook(Book book) {
        bookMapper.addBook(book);
    }

    @Override
    public PageBean<Book> list(Integer pageNum, Integer pageSize, String bookCategory, String bookName) {
        //1.创建PageBean对象
        PageBean<Book> pageBean=new PageBean<>();

        //开启分页查询PageHelper
        PageHelper.startPage(pageNum,pageSize);

        //3.调用mapper
        List<Book> books=bookMapper.list(bookCategory,bookName);

        Page<Book> p=(Page<Book>) books;

        //把数据填充到PageBean对象中
        pageBean.setTotal(p.getTotal());
        pageBean.setItems(p.getResult());
        return pageBean;
    }

    @Override
    public void updateBook(Book book) {
        bookMapper.updateBook(book);
    }

    @Override
    public void deleteBook(String bookId) {
        bookMapper.deleteBook(bookId);
    }
}
